<template>
  <el-scrollbar :height="scrollHeight?scrollHeight:isTagsview ? `calc(100vh - 154px)` : `calc(100vh - 120px)`">
    <div>
      <zh-form ref="formRef" style="width: 96%" :md="12" :lg="12" :xl="12" :model="apiInfo" :rules="rules"
               label-width="200px">
        <zh-form-input prop="funcId" label="功能ID" placeholder="" v-model="apiInfo.funcId"
                       disabled></zh-form-input>
        <zh-form-input prop="funcCode" label="功能编号" placeholder=""
                       v-model="apiInfo.funcCode" disabled></zh-form-input>
        <zh-form-input prop="funcTitle" label="功能标题" placeholder=""
                      :disabled="props.displayFlag" v-model="apiInfo.funcTitle"></zh-form-input>
        <zh-form-select
            :disabled="props.displayFlag"
            prop="funcType"
            label="功能类型"
            :options="state.dictData.funcType"
            placeholder="请选择"
            v-model="apiInfo.funcType"
        ></zh-form-select>
        <zh-form-select
            :disabled="props.displayFlag"
            prop="apiType"
            label="接口类型"
            :options="state.dictData.apiType"
            placeholder="请选择"
            v-model="apiInfo.apiType"
        ></zh-form-select>
        <zh-form-select
            :disabled="props.displayFlag"
            prop="funcSourceType"
            label="功能来源"
            :options="state.funcSourceTypeOpt"
            placeholder="请选择"
            v-model="apiInfo.funcSourceType"
        ></zh-form-select>
        <zh-form-input prop="serviceName" label="服务名" placeholder="注册到注册中心的服务名"
                       :disabled="props.displayFlag"
                       v-model="apiInfo.serviceName"></zh-form-input>
        <zh-form-input prop="qps" label="每秒调用量" placeholder="调用频次每秒并发调用量"
                       :disabled="props.displayFlag"
                       v-model="apiInfo.qps"></zh-form-input>
        <zh-form-select
            :disabled="props.displayFlag"
            prop="appId"
            :lazy="true"
            label="所属应用"
            v-model="apiInfo.appId"
            :fieldNames="{ label: 'appName', value: 'appId' }"
            funcCode="application0012"
            searchField="appName"
            @change="appIdChange"
        ></zh-form-select>
        <zh-form-select
            :disabled="props.displayFlag"
            prop="envId"
            label="应用环境"
            :options="state.envDict"
            placeholder="应用环境"
            v-model="apiInfo.envId"
        ></zh-form-select>
        <zh-form-input prop="classPath" label="所属类名" placeholder=""
                       :disabled="props.displayFlag"
                       v-model="apiInfo.classPath"></zh-form-input>
        <zh-form-input :disabled="props.displayFlag" prop="method" label="功能方法" placeholder=""
                       v-model="apiInfo.method"></zh-form-input>
        <zh-form-input prop="orderNo" label="排序" placeholder="" v-model="apiInfo.orderNo"
                       disabled></zh-form-input>
        <zh-form-select
            :disabled="props.displayFlag"
            prop="operating"
            label="操作期"
            :options="state.dictData.operating"
            placeholder="请选择"
            v-model="apiInfo.operating"
        ></zh-form-select>
        <zh-form-cascader
            :disabled="props.displayFlag"
            prop="orgId"
            label="所属单位"
            :options="state.orgDict"
            placeholder="请选择所属单位"
            :props="{checkStrictly: true,emitPath:false}"
            v-model="apiInfo.orgId"
        ></zh-form-cascader>
        <zh-form-select
            :disabled="props.displayFlag"
            prop="devOrgId"
            label="所属开发公司"
            :options="state.devOrgDict"
            placeholder="请选择所属开发公司"
            v-model="apiInfo.devOrgId"
        ></zh-form-select>
        <zh-form-select
            :disabled="props.displayFlag"
            prop="httpMethod"
            label="调用方式"
            :options="[{dictKey:'get',dictValue:'get'},{dictKey:'post',dictValue:'post'}]"
            placeholder="请选择调用方式"
            v-model="apiInfo.httpMethod"
        ></zh-form-select>
        <zh-form-input prop="timeout" label="服务超时时间" placeholder="服务超时时间(秒)"
                       :disabled="props.displayFlag" v-model="apiInfo.timeout"></zh-form-input>
        <zh-form-input prop="begDate" label="开始时间" placeholder="服务有效开始时间"
                       :disabled="props.displayFlag" v-model="apiInfo.begDate"></zh-form-input>
        <zh-form-input prop="endDate" label="结束时间" placeholder="服务有效结束时间"
                       :disabled="props.displayFlag" v-model="apiInfo.endDate"></zh-form-input>
        <zh-form-input prop="visits" label="每日次数" placeholder="每日访问上限次数"
                       :disabled="props.displayFlag" v-model="apiInfo.visits"></zh-form-input>
        <zh-form-select
            :disabled="props.displayFlag"
            prop="customCharset"
            label="自定义字符集"
            :options="state.dictData.charset"
            placeholder="请选择自定义字符集,默认为utf-8"
            v-model="apiInfo.customCharset"
        ></zh-form-select>
        <zh-form-select
            :disabled="props.displayFlag"
            prop="contentType"
            label="请求类型"
            :options="state.dictData.httpContentType"
            placeholder="请选择请求类型"
            v-model="apiInfo.contentType"
        ></zh-form-select>
        <zh-form-input prop="customClass" label="自定义处理类" placeholder="自定义处理类 需要注入"
                       :disabled="props.displayFlag" v-model="apiInfo.customClass"></zh-form-input>
        <zh-form-select
            :disabled="props.displayFlag"
            prop="webMoreFlag"
            label="webservice是否多参数"
            :options="state.dictData.flag"
            placeholder="webservice入参是否多参数"
            v-model="apiInfo.webMoreFlag"
        ></zh-form-select>
        <zh-form-select
            :disabled="props.displayFlag"
            prop="interfaceStatus"
            label="接口状态"
            :options="state.dictData.interfaceStatus"
            placeholder="接口状态"
            v-model="apiInfo.interfaceStatus"
        ></zh-form-select>
        <zh-form-select
            :disabled="props.displayFlag"
            prop="sendStatus"
            label="发送状态"
            :options="state.dictData.sendStatus"
            placeholder="发送状态"
            v-model="apiInfo.sendStatus"
        ></zh-form-select>
        <zh-form-select
            :disabled="props.displayFlag"
            prop="networkType"
            label="对外访问网络类型"
            :options="state.dictData.networkType"
            placeholder="对外访问网络类型"
            v-model="apiInfo.networkType"
        ></zh-form-select>
        <zh-form-select
            :disabled="props.displayFlag"
            prop="otherAccessFlag"
            label="是否对第三方应用开放"
            :options="state.dictData.flag"
            placeholder="是否对第三方应用开放"
            v-model="apiInfo.otherAccessFlag"
        ></zh-form-select>
        <zh-form-radio
            :disabled="props.displayFlag"
            :sm="12"
            :lg="8"
            :xl="8"
            label="是否启用"
            prop="funcStatus"
            v-model="apiInfo.funcStatus"
            :options="state.dictData.flag"
        ></zh-form-radio>
        <zh-form-radio
            :disabled="props.displayFlag"
            :sm="12"
            :lg="8"
            :xl="8"
            label="是否分发接口"
            prop="distributedFlag"
            v-model="apiInfo.distributedFlag"
            :options="state.dictData.flag"
        ></zh-form-radio>
        <zh-form-radio
            :disabled="props.displayFlag"
            :sm="12"
            :lg="8"
            :xl="8"
            label="是否组合接口"
            prop="composeFlag"
            v-model="apiInfo.composeFlag"
            :options="state.dictData.flag"
        ></zh-form-radio>
        <zh-form-radio
            :disabled="props.displayFlag"
            :sm="12"
            :lg="8"
            :xl="8"
            label="申请是否需审核"
            prop="authFlag"
            v-model="apiInfo.authFlag"
            :options="state.dictData.flag"
        ></zh-form-radio>
        <zh-form-radio
            :disabled="props.displayFlag"
            :sm="12"
            :lg="8"
            :xl="8"
            label="是否需要转义"
            prop="isEncode"
            v-model="apiInfo.isEncode"
            :options="state.dictData.flag"
        ></zh-form-radio>
        <zh-form-radio
            :disabled="props.displayFlag"
            :sm="12"
            :lg="8"
            :xl="8"
            label="是否公共"
            prop="publicFlag"
            v-model="apiInfo.publicFlag"
            :options="state.dictData.flag"
        ></zh-form-radio>
        <zh-form-radio
            :disabled="props.displayFlag"
            :sm="12"
            :lg="8"
            :xl="8"
            label="是否可回退"
            prop="rollbackFlag"
            v-model="apiInfo.rollbackFlag"
            :options="state.dictData.flag"
        ></zh-form-radio>
        <zh-form-radio
            :disabled="props.displayFlag"
            :sm="12"
            :lg="8"
            :xl="8"
            label="跨业务期回退"
            prop="spanRollbackFlag"
            v-model="apiInfo.spanRollbackFlag"
            :options="state.dictData.flag"
        ></zh-form-radio>
        <zh-form-radio
            :disabled="props.displayFlag"
            :sm="12"
            :lg="8"
            :xl="8"
            label="是否开启流程"
            prop="auditFlag"
            v-model="apiInfo.auditFlag"
            :options="state.dictData.flag"
        ></zh-form-radio>
        <zh-form-radio
            :disabled="props.displayFlag"
            :sm="12"
            :lg="8"
            :xl="8"
            label="记日志"
            prop="logFlag"
            v-model="apiInfo.logFlag"
            :options="state.dictData.flag"
        ></zh-form-radio>
        <zh-form-input
            :disabled="props.displayFlag"
            prop="api"
            :span="24"
            label="api"
            type="textarea"
            placeholder="api http的url路径，webservice·的方法名"
            v-model="apiInfo.api"
        ></zh-form-input>
        <zh-form-input
            :disabled="props.displayFlag"
            prop="classPath"
            :span="24"
            label="类路径"
            type="textarea"
            placeholder="类路径"
            v-model="apiInfo.classPath"
        ></zh-form-input>
        <zh-form-input
            :disabled="props.displayFlag"
            prop="method"
            :span="24"
            label="请求方法"
            type="textarea"
            placeholder="请求方法"
            v-model="apiInfo.method"
        ></zh-form-input>
        <zh-form-input
            :disabled="props.displayFlag"
            prop="funcDesc"
            :span="24"
            label="功能描述"
            type="textarea"
            placeholder="功能描述"
            v-model="apiInfo.funcDesc"
        ></zh-form-input>
        <zh-form-input
            :disabled="props.displayFlag"
            prop="remarks"
            :span="24"
            label="摘要信息"
            type="textarea"
            placeholder="JSONArray格式，操作日志使用，优先使用功能里的摘要，为空则使用路由里的摘要"
            v-model="apiInfo.remarks"
        ></zh-form-input>
      </zh-form>
    </div>
  </el-scrollbar>
</template>

<script lang="ts" setup name="apiForm">

import {FormInstance} from "element-plus";

const props = defineProps({
  scrollHeight: {
    type: String,
    default: ''
  },
  displayFlag: {
    type: Boolean,
    default: false
  },
});


const apiInfo = ref<any>({});

const {isTagsview} = storeToRefs($store.theme);

//------------------定义子组件---------------------------
const formRef = ref<FormInstance>();

//------------------内部变量定义-------------------------
const state = reactive({
  hideChecked: false,
  confirmLoading: false,
  dictData: {} as any,
  initData: {},
  funcSourceTypeOpt: [
    {dictKey: '1', dictValue: '内部服务'},
    {dictKey: '2', dictValue: '第三方应用接入'},
    {dictKey: '3', dictValue: '编排接口'}
  ],
  appDict: [],
  orgDict: [] as any,
  devOrgDict: [],
  envDict: []
});
const rules = reactive({
  funcSourceType: [
    {
      required: true,
      message: '请输入功能来源类型',
      trigger: 'blur',
    },
  ],
  funcStatus: [
    {
      required: true,
      message: '请输入状态',
      trigger: 'blur',
    },
  ],
  publicFlag: [
    {
      required: true,
      message: '请输入是否公共',
      trigger: 'blur',
    },
  ],
  timeout: [
    {
      required: true,
      message: '请输入是否超时时间',
      trigger: 'blur',
    }
  ],
  pathFunc: [
    {
      required: true,
      message: '请输入接口路径',
      trigger: 'blur',
    }
  ],
  visits: [
    {
      required: true,
      message: '请输入每日访问上限次数',
      trigger: 'blur',
    }
  ],
  isEncode: [
    {
      required: true,
      message: '请输入是否需要转义',
      trigger: 'blur',
    }
  ]
});


//------------------初始化------------------------------
const params = {
  dictFields: 'operating,funcType,flag,charset,httpContentType,interfaceStatus,sendStatus,networkType,apiType',
};
$api.common.getBatchDict.post(params).then((res) => {
  state.dictData = res.result;
});
$api.common.send.post('application0011', {}).then((res) => {
  state.appDict = res.result;
});
$api.common.send.post('org0033', {orgType: '0102'}).then((res) => {
  state.orgDict = res.result;
});
$api.common.send.post('org0019', {orgType: '99'}).then((res) => {
  state.devOrgDict = res.result;
});

//------------------定义调用方法-------------------------
function setInitDate(info) {
  formRef.value?.resetFields();
  if (info) {
    apiInfo.value = info;
  }
}

function appIdChange(e) {
  if (e) {
    $api.common.send.post('apiAppEnv0007', {appId: e}).then(res => {
      if (res.result) {
        state.envDict = res.result
      }
    }).catch(e => {

    });
  } else {
    state.envDict = [];
  }
}


//------------设置给父组件调用的数据/方法(组件)-----------
defineExpose({setInitDate, form: formRef, apiInfo});

</script>
